﻿namespace ApiTemplate.WebApi.Controllers
{
    /// <summary>
    /// 调度任务相关api
    /// </summary>
    public class SchedulerController : BaseApiController
    {
        /// <summary>
        /// 调度任务
        /// </summary>
        public ISchedulerApplication SchedulerApplication { get; set; }

        /// <summary>
        /// 添加一个定时任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> AddAsync(AddSchedulerTaskInput input)
        {
            return await SchedulerApplication.AddAsync(input);
        }

        /// <summary>
        /// 修改定时任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> UpdateAsync(UpdateSchedulerTaskInput input)
        {
            return await SchedulerApplication.UpdateAsync(input);
        }

        /// <summary>
        /// 获取单个定时任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<GetSchedulerTaskOutput> GetAsync([FromRoute] string id)
        {
            return await SchedulerApplication.GetAsync(id);
        }

        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> PauseTask(BaseGuidListInput input)
        {
            return await SchedulerApplication.PauseTask(input);
        }

        /// <summary>
        /// 恢复一个任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> RenewTaskAsync(BaseGuidListInput input)
        {
            return await SchedulerApplication.RenewTaskAsync(input);
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> DeleteAsync(BaseGuidListInput input)
        {
            return await SchedulerApplication.DeleteAsync(input);
        }

        /// <summary>
        /// 获取定时任务列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<GetSchedulerTaskPageOutput>> GetPageResultAsync(GetSchedulerTaskPageInput input)
        {
            return await SchedulerApplication.GetPageResultAsync(input);
        }

        /// <summary>
        /// 获取指定任务的执行日志
        /// </summary>
        /// <param name="taskId">任务id</param>
        /// <returns></returns>
        [HttpGet("{taskId}")]
        public async Task<List<SchedulerTaskLog>> GetTaskLogAsync([FromRoute] string taskId)
        {
            return await SchedulerApplication.GetTaskLogAsync(taskId);
        }
    }
}
